<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\Schedule;
use App\Models\ScheduleType;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ScheduleService
{
    public function get($search){
        $sql = Schedule::with('project')->with('user')->with('type')->when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('title','like', '%'.$search.'%');
            });
        });

        return $sql;
    }

    public function userGet($search){
        $sql = Schedule::select('id','title','project_id','type_id','person_id','floor','region','start_date','end_date','created_at')->with('type')->when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('title','like', '%'.$search.'%');
            });
        });

        return $sql;
    }

    public function statistics($id){
        $sql = Schedule::select('id','title','project_id','type_id','person_id','floor','region','start_date','end_date','created_at')->with('type')->with('records')->where('project_id',$id);
        $data = $sql->get();
        $data = $data->groupBy('region');
        $title = [];
        foreach ($data as $k => $v) {
            $title_ = [];
            foreach ($v as $key => $value) {
                $title_[] = $value->title;
            }
            $title[] = array_unique($title_);
        }
        
        $region = [];
        $res = [];
        foreach ($data as $k => $v) {
            $region[] = $k;
            $arr = [];
            $data[$k] = $v->groupBy('floor')->toArray();
            foreach ($data[$k] as $key => $value) {
                $arr[$key] = $value;
            }
            $res[$k] = $arr;
        }
        $day = Date('Y-m-d',time());
        $tomorrow = date('Y-m-d', strtotime('+1 day'));
        $config = Schedule::CONFIG;
        $res = [];
        $res['data'] = $data;
        $res['day'] = $day;
        $res['tomorrow'] = $tomorrow;
        $res['config'] = $config;
        $res['region'] = $region;
        $res['title'] = $title;
        return $res;
    }

    public function insert($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        try {
            $res['status'] = $res['status'] ?? 1;
            $data = Schedule::create($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function importInsert($input){
        $res = [];
        foreach ($input as $k => $v) {
            $v->status = 1;
            if($v){
                $res[$k] = (array)$v;
            }
        }
        try {
            $data = DB::table('schedules')->insert($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        unset($res['id']);
        try {
            $data = Schedule::where('id',$input['id'])->update($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function getType($search){
        $sql = ScheduleType::when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('title','like', '%'.$search.'%');
            });
        });

        return $sql;
    }

    public function insertType($input){
        
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        
        try {
            ScheduleType::create($res);
            return true;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function updateType($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        unset($res['id']);
        try {
            $data = ScheduleType::where('id',$input['id'])->update($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }
    
    
}